package com.softtown.modules.dapai.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.dapai.dao.CompCustomerDao;
import com.softtown.modules.dapai.dto.CompCustomerDTO;
import com.softtown.modules.dapai.dto.CustomerAddDTO;
import com.softtown.modules.dapai.entity.*;
import com.softtown.modules.dapai.service.CompCustomerContactsService;
import com.softtown.modules.dapai.service.CompCustomerMacService;
import com.softtown.modules.dapai.service.CompCustomerReceiveService;
import com.softtown.modules.dapai.service.CompCustomerService;
import com.softtown.modules.dapai.vo.CompCustomerVO;
import com.softtown.modules.dapai.vo.CustomerContactsVO;
import com.softtown.modules.oss.cloud.OSSFactory;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dto.CompComDocAttAddDTO;
import com.softtown.modules.sys.entity.CompComDocAttEntity;
import com.softtown.modules.sys.service.CompComDocAttService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CompCustomerServiceImpl extends
        BaseServiceImpl<CompCustomerDao, CompCustomerEntity> implements CompCustomerService {


    @Resource
    private CompCustomerContactsService compCustomerContactsService;

    @Resource
    private CompCustomerMacService compCustomerMacService;

    @Resource
    private CompCustomerReceiveService compCustomerReceiveService;

    @Resource
    private CompComDocAttService compComDocAttService;

    private static final String DOC_TYPE_VALUE = "comp_customer";

    @Override
    public PageInfo<CompCustomerEntity> getCustomerPageList(CompCustomerDTO compCustomerDTO) {
        Integer pageSize = compCustomerDTO.getPageSize();
        Integer page = compCustomerDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompCustomerEntity> compCustomerEntities =
                baseDao.getCustomerPageList(compCustomerDTO.getCompId(), compCustomerDTO.getParam());
        return  new PageInfo<>(compCustomerEntities);
    }

    @Override
    public CompCustomerVO getCustomerById(Long compId, Long id) {
        CompCustomerVO compCustomerVO = new CompCustomerVO();
        compCustomerVO.setCompCustomerEntity(baseDao.getCustomerById(compId, id));
        compCustomerVO.setCompCustomerContactsEntityList(baseDao.getCustomerContactsList(compId, id));
        compCustomerVO.setCompCustomerReceiveEntityList(baseDao.getCustomerReceiveList(compId, id));
        compCustomerVO.setCompCustomerMacEntityList(baseDao.getCustomerMacList(compId, id));
        // 单据附件信息
        List<CompComDocAttEntity> compComDocAttEntities = compComDocAttService.getSysDocAttEntityByDocId(
                id, compId);
        if (compComDocAttEntities.size() > 0) {
            compCustomerVO.setDocAttList(compComDocAttEntities);
        } else {
            compComDocAttEntities = new ArrayList<>();
            compCustomerVO.setDocAttList(compComDocAttEntities);
        }
return compCustomerVO;
    }

    @Override
    @Transactional
    public void addCustomer(CustomerAddDTO customerAddDTO) {
        CompCustomerEntity compCustomerEntity =ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerDTO(), CompCustomerEntity.class);
        insert(compCustomerEntity);
        customerAddDTO.getCompCustomerContactsDTOS().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        customerAddDTO.getCompCustomerReceiveDTOS().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        customerAddDTO.getCompCustomerMacDTOList().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        compCustomerContactsService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerContactsDTOS(), CompCustomerContactsEntity.class));

        compCustomerMacService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerMacDTOList(), CompCustomerMacEntity.class));

        compCustomerReceiveService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerReceiveDTOS(), CompCustomerReceiveEntity.class));
        // 添加单据附件
        if (customerAddDTO.getCompComDocAttAddDTOList() != null) {
            if (customerAddDTO.getCompComDocAttAddDTOList().size() > 0) {
                insertCompComDocAttEntity(customerAddDTO.getCompComDocAttAddDTOList(), new Date(),
                        compCustomerEntity.getId(),
                        customerAddDTO.getCompCustomerDTO().getCompId());
            }
        }
    }

    private void insertCompComDocAttEntity(List<CompComDocAttAddDTO> compComDocAttAddDTOS, Date nowDate, Long applId, Long compId) {
        // 添加单据附件
        HashSet set = new HashSet(compComDocAttAddDTOS);
        compComDocAttAddDTOS.clear();
        compComDocAttAddDTOS.addAll(set);
        for (int i = 0; i < compComDocAttAddDTOS.size(); i++) {
            CompComDocAttEntity compComDocAttEntity = new CompComDocAttEntity();
            compComDocAttEntity.setAttUrl(compComDocAttAddDTOS.get(i).getAttUrl());
            compComDocAttEntity.setCompId(compId);
            compComDocAttEntity.setAttName(compComDocAttAddDTOS.get(i).getAttName());
            compComDocAttEntity.setDocId(applId);
            compComDocAttEntity.setSctnLineNo(i + 1);
            compComDocAttEntity.setDocTypeValue(DOC_TYPE_VALUE);
            compComDocAttEntity.setCreateDate(nowDate);
            compComDocAttEntity.setCreator(SecurityUser.getUser().getId());
            compComDocAttEntity.setUpdateDate(nowDate);
            compComDocAttEntity.setUpdater(SecurityUser.getUser().getId());
            compComDocAttEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
            compComDocAttService.insert(compComDocAttEntity);
        }
    }
    @Override
    @Transactional
    public void updateCustomer(CustomerAddDTO customerAddDTO) {
        CompCustomerEntity compCustomerEntity =ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerDTO(), CompCustomerEntity.class);
        updateById(compCustomerEntity);
        // 删除 所有明细  isDeleted = 1
       List<CompCustomerContactsEntity> compCustomerContactsEntities =compCustomerContactsService.list(new QueryWrapper<CompCustomerContactsEntity>().lambda()
               .eq(CompCustomerContactsEntity::getCustId,customerAddDTO.getCompCustomerDTO().getId()));
       compCustomerContactsEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
            item.setIsDefault(IsDeleteEnum.NORMAL.value());
        });
        compCustomerContactsService.updateBatchById(compCustomerContactsEntities);

        List<CompCustomerMacEntity> compCustomerMacEntities = compCustomerMacService.list(new QueryWrapper<CompCustomerMacEntity>().lambda()
                .eq(CompCustomerMacEntity::getCustId,customerAddDTO.getCompCustomerDTO().getId()));
        compCustomerMacEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compCustomerMacService.updateBatchById(compCustomerMacEntities);

        List<CompCustomerReceiveEntity> compCustomerReceiveEntities =  compCustomerReceiveService.list(new QueryWrapper<CompCustomerReceiveEntity>().lambda()
                .eq(CompCustomerReceiveEntity::getCustId,customerAddDTO.getCompCustomerDTO().getId()));
        compCustomerReceiveEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
            item.setIsDefault(IsDeleteEnum.NORMAL.value());
        });
        compCustomerReceiveService.updateBatchById(compCustomerReceiveEntities);
        List<CompComDocAttEntity> compComDocAttEntities =
                compComDocAttService.getSysDocAttEntityByDocId(compCustomerEntity.getId(),
                        customerAddDTO.getCompCustomerDTO().getCompId());
        if (compComDocAttEntities.size() > 0) {
            for (CompComDocAttEntity compComDocAttEntity : compComDocAttEntities) {
                Objects.requireNonNull(OSSFactory.build()).deleteFile(compComDocAttEntity.getAttUrl());
            }
            // 删除单据附件
            compComDocAttService.deleteByDocId(compCustomerEntity.getId(), customerAddDTO.getCompCustomerDTO().getCompId());
        }

        // 重新塞入

        customerAddDTO.getCompCustomerContactsDTOS().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        customerAddDTO.getCompCustomerReceiveDTOS().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        customerAddDTO.getCompCustomerMacDTOList().forEach(item ->{
            item.setCompId(compCustomerEntity.getCompId());
            item.setCustId(compCustomerEntity.getId());
        });
        compCustomerContactsService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerContactsDTOS(), CompCustomerContactsEntity.class));

        compCustomerMacService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerMacDTOList(), CompCustomerMacEntity.class));

        compCustomerReceiveService.insertBatch(ConvertUtils
                .sourceToTarget(customerAddDTO.getCompCustomerReceiveDTOS(), CompCustomerReceiveEntity.class));
    }

    @Override
    @Transactional
    public void deleteCustomer(Long compId, Long id) {
        // 先删明细
        List<CompCustomerContactsEntity> compCustomerContactsEntities =compCustomerContactsService.list(new QueryWrapper<CompCustomerContactsEntity>().lambda()
                .eq(CompCustomerContactsEntity::getCustId,id));
        compCustomerContactsEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
            item.setIsDefault(IsDeleteEnum.NORMAL.value());
        });
        compCustomerContactsService.updateBatchById(compCustomerContactsEntities);

        List<CompCustomerMacEntity> compCustomerMacEntities = compCustomerMacService.list(new QueryWrapper<CompCustomerMacEntity>().lambda()
                .eq(CompCustomerMacEntity::getCustId,id));
        compCustomerMacEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compCustomerMacService.updateBatchById(compCustomerMacEntities);

        List<CompCustomerReceiveEntity> compCustomerReceiveEntities =  compCustomerReceiveService.list(new QueryWrapper<CompCustomerReceiveEntity>().lambda()
                .eq(CompCustomerReceiveEntity::getCustId,id));
        compCustomerReceiveEntities.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
            item.setIsDefault(IsDeleteEnum.NORMAL.value());
        });
        compCustomerReceiveService.updateBatchById(compCustomerReceiveEntities);

        // 删除主表
        CompCustomerEntity compCustomerEntity = baseDao.selectOne(new QueryWrapper<CompCustomerEntity>().lambda().eq(CompCustomerEntity ::getId,id)
        .eq(CompCustomerEntity::getCompId,compId));
        compCustomerEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        baseDao.updateById(compCustomerEntity);
    }

    @Override
    public CustomerContactsVO getContactsByCustId(Long compId, Long id) {


        return baseDao.getContactsByCustId(compId, id);
    }

    @Override
    public List<CompCustomerEntity> getAllCustomerList(Long compId) {
        List<CompCustomerEntity> compCustomerEntities = baseDao.selectList(new QueryWrapper<CompCustomerEntity>().lambda()
        .eq(CompCustomerEntity::getCompId,compId).eq(CompCustomerEntity::getIsDeleted,IsDeleteEnum.NORMAL.value()));
        return compCustomerEntities;
    }
}
